package com.atguigu.gmall.order.Listener.mq;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.to.ware.WareDetailsTo;
import com.atguigu.gmall.model.to.ware.WareStockStatusTo;
import com.google.common.collect.Lists;

import com.atguigu.gmall.common.constants.RabbitMqConst;
import com.atguigu.gmall.common.util.JsonUtil;
import com.atguigu.gmall.model.dto.order.OrderInfoUpdateStatusDto;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.model.to.order.OrderCreateTo;
import com.atguigu.gmall.model.to.ware.WareOrderTo;
import com.atguigu.gmall.order.mapper.OrderInfoMapper;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.atguigu.gmall.order.service.OrderService;
import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.core.type.TypeReference;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author A
 * @version 1.0
 * @description: 监听关单业务
 * @date 2024/10/13 1:33
 */
@Slf4j
@Service
public class OrderCloseListener {
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RabbitListener(queues = {RabbitMqConst.ORDER_DEAD_QUEUE})
    public void orderCloseListener(Message message, Channel channel) {


        //long deliveryTag,
        // boolean multiple,
        // boolean requeue
        OrderCreateTo orderCreateTo = null;
        try {
            orderCreateTo = JsonUtil.json2Obj(new String(message.getBody()), new TypeReference<OrderCreateTo>() {
            });
            orderInfoService.updateOrderStatus(ProcessStatus.UNPAID, ProcessStatus.CLOSED, orderCreateTo.getOrderId(), orderCreateTo.getUserId());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.error("mq发生异常,订单关单可能失败:{},发生的异常:{}", orderCreateTo, e);
        }

    }




}
